#include "OverlayMenu.h"
#include "ExecutionLog.h"
#include "GameState.h"
#include "GameWorld.h"
#include "Button.h"

// Constructor
OverlayMenu::OverlayMenu()
{
	SetActive(false);
	RecreateButtonLocations(16);
}

// Destructor
OverlayMenu::~OverlayMenu()
{
}

void OverlayMenu::Update()
{
	for(int i = 0; i < Number_of_Buttons; ++i)
		buttons[i].Update();

}

void OverlayMenu::RecreateButtonLocations(int NumberOfButtons)
{

	if(NumberOfButtons <= MAX_BUTTONS)
		Number_of_Buttons = NumberOfButtons;
	else
		Number_of_Buttons = MAX_BUTTONS;

	// 5 buttons, each button is 50 pixels tall, window height = 600
	// 600 - 250 = 350
	// space of 100 pixels @ each end = 150 / 5 -> gap of 30 pixels between each button
	// center x coordinate (width of graphic = 100)
	int y = 100, x = 0;
	
	if(Number_of_Buttons <= MAX_BUTTONS / 2)
		x = ((DataLoader->WINDOW_WIDTH / 2) - 50);
	else
		x = ((DataLoader->WINDOW_WIDTH / 2) - 100);

	int tBut = 0;
	
	// S1
	if(Number_of_Buttons > (MAX_BUTTONS / 2))
		tBut = (MAX_BUTTONS / 2);
	else
		tBut = NumberOfButtons;

	// S2 (freeDistance and requiredDistance deal with height)
	int freeDistance = DataLoader->WINDOW_HEIGHT - 200; // 100 pixels on top and bottom are not used; the distance we have that we can draw buttons on
	int requiredDistance = tBut * 50; // The distance that the buttons will occupate

	// Simple error checking using assert
	if (requiredDistance > freeDistance)
	{
		ExecuteLog->AddMessage("requiredDistance is greather than freeDistance for the overlay menu");
	}
	
	int spacing = (((freeDistance - requiredDistance) / (tBut + 1)) + 50); // amount of avaible space - the required space divided by
															 // button spaces required + 1 button height to account for button height


	for(int i = 0; i < Number_of_Buttons; ++i)
	{
		// Coordinates
		buttons[i].SetXY(x, y, x + 100, y + 50);
		//y += (30 + 50); // 30 gap, 50 tile size, spacing
		y += spacing;

		// Default Images
		buttons[i].SetImages("b_Game_Exit", "b_Game_Exit_m");

		// Default text
		buttons[i].SetText("void");

		// Have we reached the end of a column?
		if(i == ((MAX_BUTTONS / 2) - 1))
		{
			x += 100; // move x over one "button" width
			y = 100;  // reset y
		}
	}



}

// Draw (applies images to screen)
void OverlayMenu::DrawOpaq()
{
	if(isActive)	// Only draw if the OverlayMenu is active!
	{
		
		// Display "Menu"
		//TextDisplay->DisplayMessage("Menu", font, blue, (DataLoader->WINDOW_WIDTH) / 2 - 67, 55, (FONT_SIZE_LARGE));
		Key->GetFont("onuava")->drawText((DataLoader->WINDOW_WIDTH) / 2 - 67, 55, "Menu", blue);


		// Display buttons
		for(int i = 0; i < Number_of_Buttons; ++i)
		{
			buttons[i].applySurface();
			Key->GetFont("onuava")->drawText(buttons[i].GetX(), buttons[i].GetY(), buttons[i].GetText(), red);
			//TextDisplay->DisplayMessage(buttons[i].GetText(), font, red, buttons[i].GetMidX(FONT_SIZE_SMALL), buttons[i].GetMidY(FONT_SIZE_SMALL), (FONT_SIZE_SMALL));
		}
	}
}

void OverlayMenu::DrawTrans()
{
	if(isActive)
	{
		// Cover the screen in "black"
		OGL_Color RoyalBlue5 = {0, 34.0f / 255.0f, 102.0f / 255.0f, .5};
		DrawPrimitives->DrawRectangle(0, 0, DataLoader->WINDOW_WIDTH, DataLoader->WINDOW_HEIGHT, RoyalBlue5);

		// Create a "central" box
		OGL_Color col = {0, 0, (128.0f / 255.0f), 1};
		DrawPrimitives->DrawRectangle(((DataLoader->WINDOW_WIDTH / 2) - 200), 50, ((DataLoader->WINDOW_WIDTH / 2) + 200), DataLoader->WINDOW_HEIGHT - 80, col);
		DrawPrimitives->DrawRectangle(((DataLoader->WINDOW_WIDTH / 2) - 200), 50, ((DataLoader->WINDOW_WIDTH / 2) + 200), DataLoader->WINDOW_HEIGHT - 80, col, (200 / 255));


	}
}

void OverlayMenu::HandleGraphicalEvents()
{
	int x, y;
	SDL_GetMouseState(&x, &y);

	for(int i = 0; i < Number_of_Buttons; ++i)
		buttons[i].inBounds(x, y);

}

bool OverlayMenu::SetButtonMessage(string message, const int& buttonID)
{
	if(buttonID >= Number_of_Buttons)
		return false; // not a valid button

	else
	{
		// Message was set; return true
		buttons[buttonID].SetText(message);
		return true;
	}

	// Nothing fired; setting the message failed
	return false;
}

bool OverlayMenu::GetButtonBounds(const int& buttonID)
{
	if(buttonID >= Number_of_Buttons)
		return false; // not a valid button

	else
	{
		int x, y;
		SDL_GetMouseState(&x, &y);
		// Get if button was in bounds
		return buttons[buttonID].inBounds(x, y);
	}

	// Nothing fired; just return false
	return false;

}

void OverlayMenu::HandleEvents(SDL_Event& event)
{
	if(event.type == SDL_MOUSEBUTTONUP)
	{
		if(GetButtonBounds(0))
			set_next_state(STATE_MAINMENU);

		if(GetButtonBounds(1))
			set_next_state(STATE_EXIT);

		if(GetButtonBounds(2))
			WorldMap->GetGraph()->ShowEdges = !WorldMap->GetGraph()->ShowEdges;
		
		if(GetButtonBounds(3))
			World->ToggleSoundGUI();

	}

}